package com.xiaomaoguai.httpclient;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

/**
 * @Description :
 * @FileName: AbstractHttpClient.java
 * @Author :WeiHui.Zhang
 * @Data : 2015年6月10日 下午11:50:59
 * @Version:V1.00
 */
public abstract class AbstractHttpClient {

	protected final String CTX = "http://127.0.0.1:8180/pro-2/app/";

	private static final CloseableHttpClient httpclient = HttpClients.createDefault();

	public void doPostWithFile(String uri, String fileName) {
		try {
			HttpPost httppost = new HttpPost(uri);

			File uploadFile = new File(fileName);

			if (!uploadFile.isFile()) {
				System.out.println("不是文件");
			}

			FileBody bin = new FileBody(new File(fileName));

			StringBody comment = new StringBody("A binary file of some kind", ContentType.TEXT_PLAIN);

			HttpEntity reqEntity = MultipartEntityBuilder.create().addPart("bin", bin).addPart("comment", comment)
					.build();

			httppost.setEntity(reqEntity);

			System.out.println("executing request " + httppost.getRequestLine());

			CloseableHttpResponse response = httpclient.execute(httppost);

			System.out.println("----------------------------------------");
			System.out.println(response.getStatusLine());

			HttpEntity resEntity = response.getEntity();
			if (resEntity != null) {
				System.out.println("Response content length: " + resEntity.getContentLength());

				InputStream in = resEntity.getContent();
				byte[] buffer = new byte[1024];
				int readBytes;
				StringBuilder stringBuilder = new StringBuilder();
				while ((readBytes = in.read(buffer)) > 0) {
					stringBuilder.append(new String(buffer, 0, readBytes));
				}
				System.out.println(stringBuilder.toString());
			}
			EntityUtils.consume(resEntity);
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (UnsupportedOperationException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	protected void postWithFile2(String uri, String... fileName) throws ClientProtocolException, IOException {
		CloseableHttpClient httpClient = HttpClients.createDefault();

		HttpPost httpPost = new HttpPost(uri);

		MultipartEntityBuilder builder = MultipartEntityBuilder.create();

		builder.addTextBody("name", "text", ContentType.TEXT_PLAIN);

		for (String string : fileName) {
			builder.addBinaryBody("file", new File(string), ContentType.APPLICATION_OCTET_STREAM, "file.ext");
		}

		HttpEntity multipart = builder.build();

		httpPost.setEntity(multipart);

		System.out.println("executing request " + httpPost.getRequestLine());

		CloseableHttpResponse httpResponse = httpClient.execute(httpPost);

		HttpEntity entity = httpResponse.getEntity();

		if (entity != null) {
			System.out.println("Response content length: " + entity.getContentLength());
			InputStream in = entity.getContent();
			byte[] buffer = new byte[1024];
			int readBytes;
			StringBuilder stringBuilder = new StringBuilder();
			while ((readBytes = in.read(buffer)) > 0) {
				stringBuilder.append(new String(buffer, 0, readBytes));
			}
			System.out.println(stringBuilder.toString());
		}

	}
}
